home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / devel / tcl / itcl1_30.z / itcl1_30 / usr / man / mann / incrTcl.n
Encoding:
Text File  |  1993-11-16  |  30.5 KB  |  781 lines

  1. '\"
  2. '\" MAN PAGE:  [incr Tcl] - Object-oriented extensions for Tcl
  3. '\"   AUTHOR:  Michael J. McLennan
  4. '\"     SCCS:  @(#)incrTcl.n    1.3 (9/19/93)
  5. '\" ========================================================================
  6. '\"                 Copyright (c) 1993  AT&T Bell Laboratories
  7. '\" ========================================================================
  8. '\" Permission to use, copy, modify, and distribute this software and its
  9. '\" documentation for any purpose and without fee is hereby granted,
  10. '\" provided that the above copyright notice appear in all copies and that
  11. '\" both that the copyright notice and warranty disclaimer appear in
  12. '\" supporting documentation, and that the names of AT&T Bell Laboratories
  13. '\" any of their entities not be used in advertising or publicity
  14. '\" pertaining to distribution of the software without specific, written
  15. '\" prior permission.
  16. '\"
  17. '\" AT&T disclaims all warranties with regard to this software, including
  18. '\" all implied warranties of merchantability and fitness.  In no event
  19. '\" shall AT&T be liable for any special, indirect or consequential
  20. '\" damages or any damages whatsoever resulting from loss of use, data or
  21. '\" profits, whether in an action of contract, negligence or other
  22. '\" tortuous action, arising out of or in connection with the use or
  23. '\" performance of this software.
  24. '\" ========================================================================
  25. '\" 
  26. '\" Man page formatting taken from...
  27. '\" Copyright 1990 Regents of the University of California
  28. '\" Permission to use, copy, modify, and distribute this
  29. '\" documentation for any purpose and without fee is hereby
  30. '\" granted, provided that this notice appears in all copies.
  31. '\" The University of California makes no representations about
  32. '\" the suitability of this material for any purpose.  It is
  33. '\" provided "as is" without express or implied warranty.
  34. '\"
  35. .\" The definitions below are for supplemental macros used in Tcl/Tk
  36. .\" manual entries.
  37. .\"
  38. .\" .HS name section [date [version]]
  39. .\"    Replacement for .TH in other man pages.  See below for valid
  40. .\"    section names.
  41. .\"
  42. .\" .AP type name in/out [indent]
  43. .\"    Start paragraph describing an argument to a library procedure.
  44. .\"    type is type of argument (int, etc.), in/out is either "in", "out",
  45. .\"    or "in/out" to describe whether procedure reads or modifies arg,
  46. .\"    and indent is equivalent to second arg of .IP (shouldn't ever be
  47. .\"    needed;  use .AS below instead)
  48. .\"
  49. .\" .AS [type [name]]
  50. .\"    Give maximum sizes of arguments for setting tab stops.  Type and
  51. .\"    name are examples of largest possible arguments that will be passed
  52. .\"    to .AP later.  If args are omitted, default tab stops are used.
  53. .\"
  54. .\" .BS
  55. .\"    Start box enclosure.  From here until next .BE, everything will be
  56. .\"    enclosed in one large box.
  57. .\"
  58. .\" .BE
  59. .\"    End of box enclosure.
  60. .\"
  61. .\" .VS
  62. .\"    Begin vertical sidebar, for use in marking newly-changed parts
  63. .\"    of man pages.
  64. .\"
  65. .\" .VE
  66. .\"    End of vertical sidebar.
  67. .\"
  68. .\" .DS
  69. .\"    Begin an indented unfilled display.
  70. .\"
  71. .\" .DE
  72. .\"    End of indented unfilled display.
  73. .\"
  74. '\"    # Heading for Tcl/Tk man pages
  75. .de HS
  76. .if '\\$2'cmds'       .TH \\$1 1 \\$3 \\$4
  77. .if '\\$2'lib'        .TH \\$1 3 \\$3 \\$4
  78. .if '\\$2'tcl'        .TH \\$1 3 \\$3 \\$4
  79. .if '\\$2'tk'         .TH \\$1 3 \\$3 \\$4
  80. .if t .wh -1.3i ^B
  81. .nr ^l \\n(.l
  82. .ad b
  83. ..
  84. '\"    # Start an argument description
  85. .de AP
  86. .ie !"\\$4"" .TP \\$4
  87. .el \{\
  88. .   ie !"\\$2"" .TP \\n()Cu
  89. .   el          .TP 15
  90. .\}
  91. .ie !"\\$3"" \{\
  92. .ta \\n()Au \\n()Bu
  93. \&\\$1    \\fI\\$2\\fP    (\\$3)
  94. .\".b
  95. .\}
  96. .el \{\
  97. .br
  98. .ie !"\\$2"" \{\
  99. \&\\$1    \\fI\\$2\\fP
  100. .\}
  101. .el \{\
  102. \&\\fI\\$1\\fP
  103. .\}
  104. .\}
  105. ..
  106. '\"    # define tabbing values for .AP
  107. .de AS
  108. .nr )A 10n
  109. .if !"\\$1"" .nr )A \\w'\\$1'u+3n
  110. .nr )B \\n()Au+15n
  111. .\"
  112. .if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n
  113. .nr )C \\n()Bu+\\w'(in/out)'u+2n
  114. ..
  115. '\"    # BS - start boxed text
  116. '\"    # ^y = starting y location
  117. '\"    # ^b = 1
  118. .de BS
  119. .br
  120. .mk ^y
  121. .nr ^b 1u
  122. .if n .nf
  123. .if n .ti 0
  124. .if n \l'\\n(.lu\(ul'
  125. .if n .fi
  126. ..
  127. '\"    # BE - end boxed text (draw box now)
  128. .de BE
  129. .nf
  130. .ti 0
  131. .mk ^t
  132. .ie n \l'\\n(^lu\(ul'
  133. .el \{\
  134. .\"    Draw four-sided box normally, but don't draw top of
  135. .\"    box if the box started on an earlier page.
  136. .ie !\\n(^b-1 \{\
  137. \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
  138. .\}
  139. .el \}\
  140. \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
  141. .\}
  142. .\}
  143. .fi
  144. .br
  145. .nr ^b 0
  146. ..
  147. '\"    # VS - start vertical sidebar
  148. '\"    # ^Y = starting y location
  149. '\"    # ^v = 1 (for troff;  for nroff this doesn't matter)
  150. .de VS
  151. .mk ^Y
  152. .ie n 'mc \s12\(br\s0
  153. .el .nr ^v 1u
  154. ..
  155. '\"    # VE - end of vertical sidebar
  156. .de VE
  157. .ie n 'mc
  158. .el \{\
  159. .ev 2
  160. .nf
  161. .ti 0
  162. .mk ^t
  163. \h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n'
  164. .sp -1
  165. .fi
  166. .ev
  167. .\}
  168. .nr ^v 0
  169. ..
  170. '\"    # Special macro to handle page bottom:  finish off current
  171. '\"    # box/sidebar if in box/sidebar mode, then invoked standard
  172. '\"    # page bottom macro.
  173. .de ^B
  174. .ev 2
  175. 'ti 0
  176. 'nf
  177. .mk ^t
  178. .if \\n(^b \{\
  179. .\"    Draw three-sided box if this is the box's first page,
  180. .\"    draw two sides but no top otherwise.
  181. .ie !\\n(^b-1 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
  182. .el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
  183. .\}
  184. .if \\n(^v \{\
  185. .nr ^x \\n(^tu+1v-\\n(^Yu
  186. \kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c
  187. .\}
  188. .bp
  189. 'fi
  190. .ev
  191. .if \\n(^b \{\
  192. .mk ^y
  193. .nr ^b 2
  194. .\}
  195. .if \\n(^v \{\
  196. .mk ^Y
  197. .\}
  198. ..
  199. '\"    # DS - begin display
  200. .de DS
  201. .RS
  202. .nf
  203. .sp
  204. ..
  205. '\"    # DE - end display
  206. .de DE
  207. .fi
  208. .RE
  209. .sp .5
  210. ..
  211. '\"
  212. '\"    # CS - begin display of code example
  213. .de CS
  214. .RS
  215. .nf
  216. \f(CB
  217. ..
  218. '\"    # CE - end display of code example
  219. .de CE
  220. \fP
  221. .fi
  222. .RE
  223. ..
  224. .HS "[incr\ Tcl]" tcl
  225. .BS
  226. '\" Note:  do not modify the .SH NAME line immediately below!
  227. .SH NAME
  228. [incr\ Tcl] \- Object-oriented extensions to Tcl
  229. .SH SYNOPSIS
  230. \fBitcl_class \fIclassName\fR \fB{
  231. .ti +0.3i
  232. \fBinherit \fIbaseClass\fR ?\fIbaseClass\fR...?
  233. .sp 0.1i
  234. .ti +0.3i
  235. \fBconstructor \fIargs body\fR
  236. .ti +0.3i
  237. \fBdestructor \fIbody\fR
  238. .sp 0.1i
  239. .ti +0.3i
  240. \fBmethod \fIname args body\fR
  241. .ti +0.3i
  242. \fBproc \fIname args body\fR
  243. .sp 0.1i
  244. .ti +0.3i
  245. \fBpublic \fIvarName\fR ?\fIinit\fR? ?\fIconfig\fR?
  246. .ti +0.3i
  247. \fBprotected \fIvarName\fR ?\fIinit\fR?
  248. .ti +0.3i
  249. \fBcommon \fIvarName\fR ?\fIinit\fR?
  250. .br
  251. \fB}\fR
  252. .sp
  253. \fIclassName objName\fR ?\fIargs...\fR?
  254. .br
  255. \fIclassName\fR \fB#auto\fR ?\fIargs...\fR?
  256. .br
  257. \fIclassName\fR \fB::\fR \fIproc\fR ?\fIargs...\fR?
  258. .sp
  259. \fIobjName method\fR ?\fIargs...\fR?
  260. .sp
  261. \fBitcl_info classes ?\fIpattern\fR?
  262. .br
  263. \fBitcl_info objects ?\fIpattern\fR? ?\fB-class \fIclassName\fR? ?\fB-isa \fIclassName\fR?
  264. .sp
  265. \fICommands available within class methods/procs:\fR
  266. .br
  267. \fBglobal \fIvarName\fR ?\fIvarName...\fR?
  268. .br
  269. \fBprevious \fIcommand\fR ?\fIargs...\fR?
  270. .br
  271. \fBvirtual \fIcommand\fR ?\fIargs...\fR?
  272. .BE
  273.  
  274. .SH DESCRIPTION
  275. .PP
  276. \fB[incr\ Tcl]\fR provides object-oriented extensions to Tcl, much as
  277. C++ provides object-oriented extensions to C.  The emphasis of this
  278. work, however, is not to create a whiz-bang object-oriented
  279. programming environment.  Rather, it is to support more structured
  280. programming practices in Tcl without changing the flavor of the language.
  281. More than anything else, \fB[incr\ Tcl]\fR provides a means of
  282. encapsulating related procedures together with their shared data
  283. in a local namespace that is hidden from the outside world.
  284. It encourages better programming by promoting the object-oriented
  285. "library" mindset.  It also allows for code re-use through inheritance.
  286. .PP
  287. The fundamental construct in \fB[incr\ Tcl]\fR is the class definition.
  288. Each class acts as a template for actual objects that can be created.
  289. Each object in a class contains a unique bundle of data, including
  290. "public" and "protected" data members.  When execution takes place within
  291. the scope of the class, both types of data members are accessible.
  292. To the programmer working outside of the class scope\-using an object
  293. to build his application\-only public data members are accessible.
  294. Public members represent attributes that can be used to configure an
  295. object.  For example, the "-text" attribute in the usual Tk "\fBbutton\fR"
  296. widget could be thought of as a public member.  Protected members,
  297. on the other hand, reflect the inner workings of an object and are
  298. kept hidden from the outside world.  This insulates the programmer
  299. that uses a class from the details of its implementation.
  300. Classes can also define "common" data members that are shared by all
  301. objects in a class.  Like protected members, common members are only
  302. accessible within the scope of the class.  The class designer will
  303. often provide special class-level procedures or "procs" to manipulate
  304. common members.  Since these "procs" can be invoked without reference
  305. to any specific object, they have access to common members, but not
  306. to public or protected members.
  307. .PP
  308. Special procedures called "methods" are used to manipulate individual
  309. objects.  The use of methods should be familiar to any
  310. Tk programmer\-the "\fBbutton\fR" widget, for example, has methods
  311. such as "flash" and "invoke" that are used to control its behavior.
  312. These methods form a contract between the class designer
  313. and the application developer, completely specifying the list of
  314. functions that can be used to interact with objects in the class.
  315. The implementation details within each method, however, are left
  316. solely to the class designer.  The public interface says \fIwhat\fR
  317. an object will do but not \fIhow\fR it will do it.  Insulating the
  318. application developer from these details leaves the class designer
  319. free to change them at any time, without warning, and without affecting
  320. programs that rely on the class.  It is precisely this encapsulation
  321. that makes object-oriented programs easier to understand and maintain.
  322. .PP
  323. The fact that \fB[incr\ Tcl]\fR objects look like Tk widgets is no
  324. accident.  \fB[incr\ Tcl]\fR was designed this way, to blend naturally
  325. into a Tcl/Tk application.  But \fB[incr\ Tcl]\fR extends the Tk paradigm
  326. from being merely object-based to being fully object-oriented.
  327. An object-oriented system supports inheritance, allowing classes to
  328. share common behaviors by inheriting them from an ancestor or
  329. base class.  Having a base class as a common abstraction allows
  330. a programmer to treat related classes in a similar manner.  For
  331. example, a toaster and a blender perform different (specialized)
  332. functions, but both share the abstraction of being appliances.
  333. By abstracting common behaviors into a base class, code can be
  334. \fIshared\fR rather than \fIcopied\fR.  The resulting application
  335. is easier to understand and maintain, and derived classes (e.g.,
  336. specialized appliances) can be added or removed more easily.
  337. .PP
  338. This description was merely a brief overview of object-oriented
  339. programming and \fB[incr\ Tcl]\fR.  A more tutorial introduction is
  340. presented in the paper included with this distribution.  Further
  341. details are presented in reference form below.
  342.  
  343. .SH SCOPE
  344. .PP
  345. Each class maintains its own local scope, separate from the main
  346. interpreter.  Within the scope of a class, all members\-including
  347. methods and procs, as well as public, protected and common
  348. variables\-can be accessed transparently.  In other words,
  349. methods and procs can be used like ordinary commands, and variables
  350. can be used without declaring them with anything like the Tcl
  351. "\fBglobal\fR" command.
  352. .PP
  353. The scope of a class extends to all derived classes as well, so members
  354. can be accessed transparently throughout a derivation hierarchy.
  355. If the same member name appears more than once in the hierarchy,
  356. the class with highest priority gets transparent access to the simple
  357. member name, and all other members must be accessed using the explicit
  358. "\fIclass\fR::\fImember\fR" syntax.  Priority is determined by the
  359. list of classes as reported by the "\fBinfo heritage\fR" command.
  360. .PP
  361. If a command is not recognized in a particular class scope, it is
  362. passed up the derivation hierarchy for evaluation.  Again, base classes
  363. are consulted in the order reported by the "\fBinfo heritage\fR"
  364. command.
  365. If a command is not recognized by any class in the hierarchy, it is
  366. passed out to the global scope (main interpreter) for evaluation.
  367. This scheme allows Tk widget commands, for example, to be accessed
  368. transparently within any class, but to be executed (as they should be)
  369. in the global scope.  Any command can be sent explicitly to the global
  370. scope using the "::\fIcommand\fR" syntax.
  371. .PP
  372. It is sometimes convenient, particularly when dealing with Tk widgets,
  373. to have access to global variables that exist in the main interpreter.
  374. This is achieved using the usual Tcl "\fBglobal\fR" command
  375. within a class method or proc.
  376.  
  377. .SH ARGUMENT LISTS
  378. .PP
  379. Class methods and procs look like ordinary Tcl procs, except that they
  380. also provide transparent access to class data.  The rules for handling
  381. formal arguments in the \fIargs\fR lists are the same as well.  Each
  382. element of this list can be a simple argument name, or another list
  383. containing the argument name and its default value.  If the last formal
  384. argument is named "args", it will absorb all unmatched arguments into
  385. a single list of values.
  386. .PP
  387. Argument lists for class methods also recognize a special argument to
  388. handle configuration of public variables.  If the last formal argument
  389. is named "config", it will absorb all unmatched arguments and parse
  390. them as "\-\fIvarName value\fR" assignments.  If \fIvarName\fR is
  391. recognized as a public variable, then this variable is modified to
  392. contain the given value.  If the variable was defined with special
  393. \fIconfig\fR code, this code is automatically executed in the scope
  394. of its associated class.  If this \fIconfig\fR code returns an error,
  395. the variable
  396. is automatically reset to its previous value, and the method is aborted,
  397. returning the configuration error.  If argument parsing is successful,
  398. the "config" variable is set to the list of variables that were
  399. configured, and the method body is executed.
  400. .PP
  401. As an example, suppose that we define a simple class with two public
  402. variables:
  403. .CS
  404. itcl_class Thing {
  405.     constructor {config} {}
  406.     method configure {config} {}
  407.  
  408.     public foo "" { puts stdout "-- just modified foo: $foo" }
  409.     public bar "" { puts stdout "-- just modified bar: $bar" }
  410. }
  411. .CE
  412. Both the constructor and the "configure" method contain the special
  413. "config" formal argument.  Thus, either of these methods can be used
  414. to configure the public variables:
  415. .CS
  416. wish: Thing x -foo 1 -bar 2
  417. -- just modified foo: 1
  418. -- just modified bar: 2
  419. x
  420. wish: x configure -bar 3
  421. -- just modified bar: 3
  422. .CE
  423. Each time "foo" or "bar" is configured in this example, its special
  424. \fIconfig\fR code (last argument in the "public" declaration) is executed,
  425. printing a message to show the updated value.
  426.  
  427. .SH CLASS DEFINITIONS
  428. .TP
  429. \fBitcl_class \fIclassName definition\fR
  430. Provides the definition for a class named \fIclassName\fR.  If
  431. \fIclassName\fR is already defined, then this command returns
  432. an error.  If the class definition is successfully parsed,
  433. \fIclassName\fR becomes a command in the interpreter, handling the
  434. creation of objects and providing access to class scope.
  435. The class \fIdefinition\fR
  436. is evaluated as a series of Tcl statements that configure the interpreter
  437. managing a particular class scope.  In addition to the usual
  438. commands, the following class definition commands are recognized:
  439. .RS
  440. .TP
  441. \fBinherit \fIbaseClass\fR ?\fIbaseClass\fR...?
  442. Declares one or more base classes, causing the current class to
  443. inherit their characteristics.  Classes must have been defined by
  444. a previous "\fBitcl_class\fR" command, or must be available to the
  445. auto-loading facility (see "AUTO-LOADING" below).  A single class
  446. definition can contain no more than one "\fBinherit\fR" command.
  447. .RS
  448. .LP
  449. When the same member name appears in two or more base classes,
  450. the base class that appears first in the "\fBinherit\fR" list takes
  451. precedence.  For example, if classes "Foo" and "Bar" both contain
  452. the member "x", then the "\fBinherit\fR" statement:
  453. .CS
  454. inherit Foo Bar
  455. .CE
  456. allows "Foo::x" to be accessed simply as "x" but forces "Bar::x" (and
  457. all other inherited members named "x") to be referenced with their
  458. explicit "\fIclass\fR::\fImember\fR" name.
  459. .RE
  460. .TP
  461. \fBconstructor \fIargs body\fR
  462. Declares the argument list and body used for the constructor, which
  463. is automatically invoked whenever an object is created.  If construction
  464. is successful, the constructor always returns the object name\-regardless
  465. of how the \fIbody\fR is defined\-and the object name becomes a command
  466. in the main interpreter.  If construction fails, an error message is
  467. returned.
  468. .RS
  469. .LP
  470. Like any other method, the constructor can be inherited
  471. from a base class.  Furthermore, any base class constructor that is
  472. not explicitly invoked within \fIbody\fR will be implicitly invoked
  473. by \fB[incr\ Tcl]\fR in the order that the base classes are reported
  474. in the "\fBinfo heritage\fR" command.  This ensures that all base classes
  475. are properly constructed.
  476. .RE
  477. .TP
  478. \fBdestructor \fIbody\fR
  479. Declares the body used for the destructor, which is automatically invoked
  480. whenever an object is deleted.  If the destructor is successful, the object
  481. data is destroyed and the object name is removed as a command from the
  482. main interpreter.  If destruction fails, an error message is returned
  483. and the object remains.
  484. .RS
  485. .LP
  486. Like any other method, the destructor can be inherited from a base class.
  487. Furthermore, any base class destructor that is not explicitly invoked
  488. within \fIbody\fR will be implicitly invoked by \fB[incr\ Tcl]\fR, in the
  489. \fIreverse\fR order compared to constructors.
  490. .RE
  491. .TP
  492. \fBmethod \fIname args body\fR
  493. Declares a method called \fIname\fR with an argument list \fIargs\fR
  494. and a \fIbody\fR of Tcl statements.  A method is just like the usual
  495. Tcl "proc" except that it has transparent access to public, protected
  496. and common variables.  Within the class scope, a method can be invoked
  497. like any other command\-simply by using its name.  In the
  498. external interpreter, the method name must be prefaced by an object
  499. name.  Methods in a base class that are redefined in the current class
  500. or hidden by another base class can be explicitly scoped using the
  501. "\fIclass\fR::\fImethod\fR" syntax.
  502. .TP
  503. \fBproc \fIname args body\fR
  504. Declares a proc called \fIname\fR with an argument list \fIargs\fR
  505. and a \fIbody\fR of Tcl statements.  A proc is similar to a method,
  506. except that it can be invoked without referring to a specific object,
  507. and therefore has access only to common variables\-not to public or
  508. protected variables.  Within the class scope, a proc can be invoked
  509. like any other command\-simply by using its name.  In the
  510. external interpreter, the proc is invoked using the \fIclassName\fR
  511. command "\fIclassName\fR :: \fIproc\fR" to access the class scope
  512. (see below).  Procs in a base class that are redefined in the current
  513. class or hidden by another base class can be explicitly scoped using
  514. the "\fIclass\fR::\fIproc\fR" syntax.
  515. .TP
  516. \fBpublic \fIvarName\fR ?\fIinit\fR? ?\fIconfig\fR?
  517. Declares a public variable named \fIvarName\fR.  Public variables are
  518. visible in methods within the scope of their class and any derived class.
  519. In addition, they can be modified outside of the class scope using the special
  520. "config" formal argument (see "ARGUMENT LISTS" above).  If the optional
  521. \fIinit\fR is specified, it is used as the initial value of the variable
  522. when a new object is created.  If the optional \fIconfig\fR command
  523. is specified,
  524. it is invoked whenever a public variable is modified via the "config"
  525. formal argument; if the \fIconfig\fR command returns an error, the
  526. public variable is reset to its value before configuration, and the
  527. method handling the configuration returns an error.
  528. .TP
  529. \fBprotected \fIvarName\fR ?\fIinit\fR?
  530. Declares a protected variable named \fIvarName\fR.  Protected variables
  531. are visible in methods within the scope of their class and any derived class,
  532. but cannot
  533. be modified outside of the class scope.  If the optional \fIinit\fR
  534. is specified, it is used as the initial value of the variable when a new
  535. object is created.  Initialization forces the variable to be a simple
  536. scalar value; uninitialized variables, on the other hand, can be used
  537. as arrays.  All objects have a built-in protected variable named
  538. "this" which is initialized to the instance name for the object.
  539. .TP
  540. \fBcommon \fIvarName\fR ?\fIinit\fR?
  541. Declares a common variable named \fIvarName\fR.  Common variables are
  542. shared among all objects in a class.  They are visible in methods and
  543. procs in the scope of their class and any derived class, but cannot be
  544. modified outside of the class scope.
  545. If the optional \fIinit\fR is specified, it is used as the
  546. initial value of the variable.  Initialization forces the variable to be
  547. a simple scalar value; uninitialized variables, on the other hand, can
  548. be used as arrays.
  549. .RS
  550. .LP
  551. Once a common variable has been declared, it can be configured using
  552. ordinary Tcl code within the class definition.  This facility is
  553. particularly useful when the initialization of the variable is
  554. non-trivial\-when the variable contains an array of values, for example:
  555. .CS
  556. itcl_class Foo {
  557.      .
  558.      .
  559.     common boolean
  560.     set boolean(true) 1
  561.     set boolean(false) 0
  562. }
  563. .CE
  564. .RE
  565. .RE
  566.  
  567. .SH CLASS USAGE
  568. .PP
  569. When a class definition has been loaded (or made available to the
  570. auto-loader), it can be used as a command in the main interpreter:
  571. .TP
  572. \fIclassName objName\fR ?\fIargs...\fR?
  573. Creates a new object in class \fIclassName\fR with the name \fIobjName\fR.
  574. Remaining arguments are passed to the constructor.  If construction is
  575. successful, the object name is returned and this name becomes a command
  576. in the main interpreter.  Otherwise, an error is returned.
  577. .TP
  578. \fIclassName\fR #auto ?\fIargs...\fR?
  579. Creates a new object in class \fIclassName\fR with an automatically
  580. generated name.  Names are of the form \fIclassName<number>\fR, e.g.,
  581. Toaster0, Toaster1, etc.  Remaining arguments are passed to the constructor.
  582. If construction is successful, the object name is returned and this name
  583. becomes a command in the main interpreter.  Otherwise, an error is returned.
  584. .TP
  585. \fIclassName\fR  ::  \fIproc\fR ?\fIargs...\fR?
  586. Used at the global scope to invoke a class proc named \fIproc\fR.
  587. Class procs are like ordinary Tcl procs, except that they are executed
  588. in the scope of the class and therefore have transparent
  589. access to common data members.
  590. .RS
  591. .LP
  592. Notice that, unlike any other scope qualifier in \fB[incr\ Tcl]\fR, the "::"
  593. shown above is surrounded by spaces.  This is necessary to avoid
  594. polluting the global namespace with every possible "\fIclass\fR::\fIproc\fR"
  595. command.
  596. .VS
  597. In Release 1.1, a special version of the usual "unknown" proc was provided
  598. to recognize commands like "\fIclass\fR::\fIproc\fR" (without spaces around
  599. the "::" qualifier).  This facility was unpopular, and is no longer supported.
  600. When class procs are invoked from the main interpreter, the "::" qualifier
  601. must be surrounded by spaces.
  602. .VE
  603. .RE
  604.  
  605. .SH OBJECT USAGE
  606. .TP
  607. \fIobjName method\fR ?\fIargs...\fR?
  608. Invokes a method named \fImethod\fR to operate on the specified object.
  609. Remaining arguments are passed to the method.  The method name can
  610. be "constructor", "destructor", any method name appearing in the
  611. class definition, or any of the following built-in methods.
  612. .SH BUILT-IN METHODS
  613. .TP
  614. \fIobjName\fR \fBisa \fIclassName\fR
  615. Returns non-zero if the given \fIclassName\fR can be found in the
  616. object's heritage, and zero otherwise.
  617. .TP
  618. \fIobjName\fR \fBdelete\fR
  619. Invokes the destructor associated with an object.
  620. If the destructor is successful, data associated with the object is
  621. deleted and \fIobjName\fR is removed from the commands in the main
  622. interpreter.  Returns the empty string, regardless of the destructor
  623. body.
  624. .TP
  625. \fIobjName\fR \fBinfo \fIoption\fR ?\fIargs...\fR?
  626. .TP
  627. \fIclassName\fR :: \fBinfo \fIoption\fR ?\fIargs...\fR?
  628. Returns information related to the class definition, or information
  629. concerning the interpreter that implements the class scope.  The
  630. \fIoption\fR parameter includes the following things, as well as
  631. the options recognized by the usual Tcl "info" command:
  632. .RS
  633. .TP
  634. \fIobjName\fR \fBinfo class\fR
  635. Returns the class name at the current class scope.  When prefaced
  636. by the object name, this command executes in the most-specific
  637. class scope, and therefore returns the most-specific class name.
  638. .TP
  639. \fIobjName\fR \fBinfo inherit\fR
  640. Returns the list of base classes as they were defined in the
  641. "\fBinherit\fR" command, or an empty string if this is a top-level class.
  642. .TP
  643. \fIobjName\fR \fBinfo heritage\fR
  644. Returns the current class name and the entire list of base classes in
  645. the order that they are traversed for object construction or member
  646. lookup.
  647. .TP
  648. \fIobjName\fR \fBinfo method\fR
  649. .TP
  650. \fIobjName\fR \fBinfo method\fR \fImethodName\fR ?\fB-args\fR? ?\fB-body\fR?
  651. In the first form, this command returns a list of all class methods.
  652. In the second form, it returns information for a specific method.
  653. If neither of the optional \fB-args\fR or \fB-body\fR flags is specified,
  654. a complete method definition is returned as a list of three elements
  655. including the method name, argument list and body.  Otherwise, the
  656. requested information is returned without the method name.
  657. If the \fImethodName\fR is not recognized, an empty string is returned.
  658. .TP
  659. \fIobjName\fR \fBinfo public\fR
  660. .TP
  661. \fIobjName\fR \fBinfo public\fR \fIvarName\fR ?\fB-init\fR? ?\fB-value\fR? ?\fB-config\fR?
  662. In the first form, this command returns a list of all public variables.
  663. In the second form, it returns information for a specific public variable.
  664. If none of the optional \fB-init\fR, \fB-value\fR or \fB-config\fR flags
  665. are specified, all available information is returned as a list of four
  666. elements including the variable name, initial value, current value,
  667. and configuration commands.  Otherwise, the requested information is
  668. returned without the variable name.
  669. If the \fIvarName\fR is not recognized, an empty string is returned.
  670. .TP
  671. \fIobjName\fR \fBinfo protected\fR
  672. .TP
  673. \fIobjName\fR \fBinfo protected\fR \fIvarName\fR ?\fB-init\fR? ?\fB-value\fR?
  674. In the first form, this command returns a list of all protected variables.
  675. In the second form, it returns information for a specific protected variable.
  676. If neither of the optional \fB-init\fR or \fB-value\fR flags is specified,
  677. all available information is returned as a list of three elements
  678. including the variable name, initial value and current value.
  679. Otherwise, the requested information is returned without the variable name.
  680. If the \fIvarName\fR is not recognized, an empty string is returned.
  681. .TP
  682. \fIobjName\fR \fBinfo common\fR
  683. .TP
  684. \fIobjName\fR \fBinfo common\fR \fIvarName\fR ?\fB-init\fR? ?\fB-value\fR?
  685. In the first form, this command returns a list of all common variables.
  686. In the second form, it returns information for a specific common variable.
  687. If neither of the optional \fB-init\fR or \fB-value\fR flags is specified,
  688. all available information is returned as a list of three elements
  689. including the variable name, initial value and current value.
  690. Otherwise, the requested information is returned without the variable name.
  691. If the \fIvarName\fR is not recognized, an empty string is returned.
  692. .RE
  693. .SH CLASS/OBJECT INFORMATION
  694. .PP
  695. The following commands are available at the global scope to query
  696. information about classes and objects that have been created.
  697. .TP
  698. \fBitcl_info classes ?\fIpattern\fR?
  699. Returns a list of classes with names matching the string \fIpattern\fR
  700. according to the rules of the "\fBstring match\fR" command.  If the
  701. optional \fIpattern\fR is not specified, a list of all known classes
  702. is returned.
  703. .TP
  704. \fBitcl_info objects ?\fIpattern\fR? ?\fB-class \fIclassName\fR? ?\fB-isa \fIclassName\fR?
  705. Returns a list of objects with names matching the string \fIpattern\fR
  706. according to the rules of the "\fBstring match\fR" command.  If the
  707. optional \fIpattern\fR is not specified, a list of all known objects
  708. is returned.  If the optional "\fB-class\fR" parameter is specified,
  709. this list is restricted to objects whose most-specific class is
  710. \fIclassName\fR.  If the optional "\fB-isa\fR" parameter is specified,
  711. this list is further restricted to objects having the given \fIclassName\fR
  712. anywhere in their heritage.
  713. .SH OTHER BUILT-IN COMMANDS
  714. The following commands are also available within the scope of each class.
  715. They cannot be accessed from outside of the class as proper methods or
  716. procs; rather, they are useful inside the class when implementing its
  717. functionality.
  718. .TP
  719. \fBglobal \fIvarName\fR ?\fIvarName...\fR?
  720. Creates a link to one or more global variables in the main interpreter.
  721. This is useful when communicating with Tk widgets that rely on global
  722. variables.
  723. .TP
  724. \fBprevious \fIcommand\fR ?\fIargs...\fR?
  725. Invokes \fIcommand\fR in the scope of the most immediate base class
  726. .VS
  727. (i.e., the "previous" class) for the object.  For classes using single
  728. .VE
  729. inheritance, this facility can be used to avoid hard-wired base class
  730. references of the form "\fIclass\fR::\fIcommand\fR", making code easier
  731. to maintain.  For classes using multiple inheritance, the utility of
  732. this function is dubious.
  733. If the class at the relevant scope has no base class, an error is returned.
  734. .VS
  735. .TP
  736. \fBvirtual \fIcommand\fR ?\fIargs...\fR?
  737. Invokes \fIcommand\fR in the scope of the most-specific class for the
  738. object.  This provides a way of accessing "virtual" functions from
  739. within class methods.  Normally, the commands at a certain class scope
  740. refer to methods/procs in that class or in less-specific base classes
  741. upward in the hierarchy.  The \fBvirtual\fR command moves the scope
  742. \fIdownward\fR to the most-specific class in the hierarchy, and then
  743. invokes the method.  If a class has methods that are redefined in
  744. derived classes, this causes the most-specific method to be invoked.
  745. Note that this is only needed when commands are invoked from within a
  746. class; when commands are invoked at the global scope, they are
  747. automatically executed in the scope of the most-specific class.
  748. .VE
  749.  
  750. .SH AUTO-LOADING
  751. .PP
  752. Class definitions need not be loaded explicitly; they can be loaded as
  753. needed by the usual Tcl auto-loading facility.  Each directory containing
  754. class definition files should have an accompanying "tclIndex" file.
  755. Each line in this file identifies a Tcl procedure \fIor [incr\ Tcl]
  756. class definition\fR and the file where the definition can be found.
  757. .PP
  758. For example, suppose a directory contains the definitions for classes
  759. "Toaster" and "SmartToaster".  Then the "tclIndex" file for this
  760. directory would look like:
  761. .CS
  762. # Tcl autoload index file: each line identifies a Tcl
  763. # procedure or [incr Tcl] class and the file where that
  764. # entity is defined.
  765. Toaster Toaster.tcl
  766. SmartToaster SmartToaster.tcl
  767. .CE
  768. The auto-loader must be made aware of this directory by appending
  769. the directory name to the "auto_path" variable.  When this is in
  770. place, classes will be auto-loaded as needed when used in an
  771. application.
  772. .PP
  773. Note that the usual "auto_mkindex" procedure used to generate the
  774. "tclIndex" file does not, by default, recognize \fB[incr\ Tcl]\fR
  775. class definitions; however, it can be modified to do so.  Such a
  776. modification is included in the "library" directory in the usual
  777. distribution for \fB[incr\ Tcl]\fR.
  778.  
  779. .SH KEYWORDS
  780. object-oriented, class
  781.